<?php

namespace App\Logging\Logger;

use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

class SqlJsonLogger
{
    protected $name = 'sql_json';

    /**
     * Create a custom Monolog instance.
     *
     * @param  array  $config
     * @return \Monolog\Logger
     */
    public function __invoke(array $config)
    {
        $logger = new Logger($this->name);

        // 添加 handler
        $handler = new RotatingFileHandler($config['path'], $config['days'], $config['level']);
        $handler->setFormatter(new JsonFormatter());
        $logger->pushHandler($handler);

        // 添加 processor
        if (isset($config['processor'])) {
            foreach ($config['processor'] as $processor) {
                $logger->pushProcessor(new $processor);
            }
        }

        return $logger;
    }
}
